package my.mashup;

import java.io.*;
import java.util.*;

public class RelMap {
    public RelMap() {

    }

    public final Map<String, Api> apiByAddr = new HashMap<>();
    public final Map<Integer, Api> apiById = new HashMap<>();
    public final Map<String, Set<Api>> apiByCate = new HashMap<>();

    public void genApi() throws IOException {
        File file = new File("./data/api_nodes_estimator.csv");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // 遍历文件每一行
        String s;
        reader.readLine(); // 第一行不用
        int id = 1;
        while ((s = reader.readLine()) != null) {
            Api api = new Api();
            String[] info = s.split("\t");
            api.id = id++;
            api.addr = info[1];
            api.cate = info[6].toLowerCase();
            String name = info[2].substring(0, info[2].length() - 18).toLowerCase(); // Mashup 名称，去除后缀，转换成小写增加识别率
            // 去除多余信息
            if (name.length() > 5 && name.endsWith("rest")) {
                name = name.substring(0, name.length() - 5);
            }
            if (name.length() > 5 && name.endsWith("jrpc")) {
                name = name.substring(0, name.length() - 5);
            }
            if (name.length() > 5 && name.endsWith("grpc")) {
                name = name.substring(0, name.length() - 5);
            }
            if (name.length() > 8 && name.endsWith("xml-grpc")) {
                name = name.substring(0, name.length() - 8);
            }
            if (name.length() > 9 && name.endsWith("json-rpc")) {
                name = name.substring(0, name.length() - 9);
            }
            if (name.length() > 4 && name.endsWith("rpc")) {
                name = name.substring(0, name.length() - 4);
            }
            api.name = name;
            apiByAddr.put(api.addr, api);
            apiById.put(api.id, api);
            if (apiByCate.containsKey(api.cate)) {
                Set<Api> temp = apiByCate.get(api.cate);
                temp.add(api);
            } else {
                Set<Api> temp = new HashSet<>();
                temp.add(api);
                apiByCate.put(api.cate, temp);
            }
        }
    }

    public final Map<String, String> mashupToCat = new HashMap<>();

    public void genMashup() throws IOException {
        File file = new File("./data/mashup_nodes_estimator.csv");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // 遍历文件每一行
        String s;
        reader.readLine(); // 第一行不用
        while ((s = reader.readLine()) != null) {
            String[] info = s.split("\t");
            if (info[1].equals("Undefined")) { // 失效 Mashup 去之不用
                continue;
            }
            String name = info[2].substring(8).toLowerCase(); // Mashup 名称，去除前缀，转换成小写增加识别率
            mashupToCat.put(name, info[6]);
        }
    }

    public final Map<String, Set<String>> mashupToAddrs = new HashMap<>();

    public void genRel() throws IOException {
        File file = new File("./data/m-a_edges.csv");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // 遍历文件每一行
        String s;
        reader.readLine(); // 第一行不用
        while ((s = reader.readLine()) != null) {
            String[] info = s.split("\t");
            String name = info[0].substring(8).toLowerCase(); // Mashup 名称，去除前缀，转换成小写增加识别率
            String addr = info[1]; // API addr
            if (mashupToAddrs.containsKey(name)) {
                Set<String> arr = mashupToAddrs.get(name);
                arr.add(addr);
            } else {
                Set<String> arr = new HashSet<>();
                arr.add(addr);
                mashupToAddrs.put(name, arr);
            }
        }
    }

    public void genAll() throws IOException {
        genApi();
        genMashup();
        genRel();
    }
}
